<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/16
 * Time: 16:05
 * 帐户列表
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\AccountType;
use app\common\model\FundsType;
use app\common\model\UserBind;
use app\common\validate\UserAccount;
use think\Db;

class Account extends Api {
	protected $noNeedRight = ['*'];
	
	
	/**
	 * 帐户列表
	 */
	public function index(){
		$userId = $this->auth->id;
		$targetUserId = input('post.target_user_id', '', 'intval');
		
		//如果有指定特定用户ID，查看指定用户的帐户
		if($targetUserId) {
			$where['ua.user_id'] = $targetUserId;
		} else {
			//默认查看自己和绑定用户的帐户
			$bindUsers = Db::name('userBind')
				->where(['user_id' => $userId, 'status' => UserBind::STATUS_Y])
				->column('target_user_id', 'id');
			$bindUsers[] = $userId;
			$where['ua.user_id'] = ['in', $bindUsers];
		}
		
		$rows = Db::name('userAccount')->alias('ua')
			->join('accountType t', 'ua.account_type_id = t.id', 'left')
			->join('userBind u', 'ua.user_id = u.target_user_id and u.user_id = '.$userId, 'left')
			->join('funds f', 'ua.id = f.account_id', 'left')
			->where($where)
			->field('ua.id, ua.user_id, ua.balance, ua.name, t.logo_image, t.bg_color, u.remark')
			->order(['ua.user_id' => 'ASC', 'f.create_time' => 'DESC', 'ua.update_time' => 'DESC'])
			->column('ua.id, ua.user_id, ua.balance, ua.name, t.logo_image, t.bg_color, u.remark, from_unixtime(f.create_time) as date', 'ua.id');
		
		$amount = 0;
		if($rows) {
			$rows = array_filter($rows);
			foreach ($rows as $row) {
				$amount += $row['balance'];
			}
		}
		
		$returnData = [
			'amount' => $amount,
			'account_list' => array_values($rows)
		];
		
		$this->success('查询成功', $returnData);
	}
	
	
	/**
	 * 帐户类型列表
	 */
	public function accountTypeList(){
		$model = new AccountType();
		$rows = Db::name('accountType')
			->where(['status' => AccountType::STATUS_Y])
			->order(['sort' => 'ASC'])
			->field('id, name, logo_image')
			->select();
		
		$this->success('查询成功', $rows);
	}
	
	/**
	 * 添加帐户
	 */
	public function add(){
		$data = input('post.');
		
		$data['user_id'] = $this->auth->id;
		
		$validate = new UserAccount();
		$validateRes = $validate->check($data);
		if(!$validateRes) {
			$this->error($validate->getError());
		}
		
		$model = new \app\common\model\UserAccount();
		$res = $model->allowField(true)->isUpdate(false)->save($data);
		if($res) {
			$this->success('添加成功');
		} else {
			$this->error('添加出错，请稍候重试');
		}
	}
	
	
	/**
	 * 获取资金流水类型
	 */
	public function getFundsType(){
		$model = new FundsType();
		
		$type = input('post.type', $model::TYPE_LESS, 'intval');
		if(!in_array($type, array_keys($model::$typeList))) {
			$type = $model::TYPE_LESS;
		}
		
		$rows = Db::name('fundsType')->alias('t')
			->join('funds f', 't.id = f.funds_type_id', 'left')
			->field('t.id, t.name, t.icon_image')
			->where(['t.type' => $type])
			->order(['f.create_time' => 'DESC', 't.sort' => 'ASC'])
			->group('t.id')
			->select();
		
		$this->success('查询成功',$rows);
	}
}